<?php
/**
 * Created by PhpStorm.
 * User: hooshim
 * Date: 2014-12-03
 * Time: 오전 11:23
 */

header("Content-Type:text/html;charset=utf-8");
header("Cache-Control:no-cache");
header("Pragma:no-cache");

include_once ($_SERVER['DOCUMENT_ROOT']."/inc/conn.inc.php");
include_once ($_SERVER['DOCUMENT_ROOT']."/inc/function.inc.php"); ##property 파일 인크루드됨.

	//## 파라미터 선언
    $findCompany 			= $_POST['findCompany'];

	// Table 이름 선언
	$tbname = "tblPayment";

    $curDt =date("Y-m-d");

    $query = "SELECT companyKR, REPLACE(CONVERT(varchar, CAST(SUM(amt) AS money), 1), '.00', '') amt ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST((SUM(amt)-SUM(payAmt)) AS money), 1), '.00', '') restAmt ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST(SUM(payAmt) AS money), 1), '.00', '') payAmt, compCd as LINK FROM ( ";
    $query .= "		SELECT SUM(A.amount) amt, 0 payAmt, C.companyKR, C.compCd ";
    $query .= "			 FROM {$tbname} A LEFT JOIN tblCompany C ON A.compCd=C.compCd ";
    $query .= "		WHERE A.flag='Y' ";
    $query .= "		GROUP BY C.companyKR, C.compCd ";
    $query .= "		UNION ALL ";
    $query .= "		SELECT 0 amt, SUM(B.amount) payAmt, C.companyKR, C.compCd ";
    $query .= "          FROM {$tbname} A LEFT JOIN tblPayResult B ON  A.payCd=B.payCd ";
    $query .= "				LEFT JOIN tblCompany C ON A.compCd=C.compCd ";
    $query .= "		WHERE A.flag='Y' AND B.flag='Y' ";
    $query .= "		GROUP BY C.companyKR, C.compCd ";
    $query .= ") P ";
    $query .= "GROUP BY companyKR, compCd ";
    $query .= "ORDER BY companyKR ";

    $logger->debug($query ."<br>[#FROM:" . $G_remote_ip."#]"); ## 쿼리 로그 작성
    $result = makeJSONArray($query,"GRIDDATA");

    $output = json_encode($result);
    $output = urldecode($output);
    echo iconvDecode($output);
    $logger->debug($_SESSION['BID'] ."//" .$output ."<br>[#FROM:" . $G_remote_ip."#]"); ## 결과 로그 작성
?>